/*
 * @Author: your name
 * @Date: 2021-10-27 09:55:16
 * @LastEditTime: 2021-11-03 11:24:24
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /egg_ payment/app/controller/home.js
 */
'use strict';

const Controller = require('egg').Controller;
let OSS = require('ali-oss');

let aliInfo = {
    // https://help.aliyun.com/document_detail/31837.html
    region: 'oss-cn-beijing',
    bucket: 'zhuxiaodong123',
    accessKeyId: 'LTAI5tS9v7vXuhpiY1VNwijr',
    accessKeySecret: 'XWt754vXSGKrpeXixUgYnERJYcqaqy'
}

let client = new OSS(aliInfo);

class AliOssController extends Controller {
  async aliOss() {
    const { ctx } = this;
    const file = ctx.request.files[0];
    let result;
    try {
      // https://help.aliyun.com/document_detail/111265.html
      // 处理文件，比如上传到云端
      result = await client.put(file.filename, file.filepath);
      ctx.body = {
        url: result.url,
        // 获取所有的字段值
        requestBody: result,
      };
    } catch (err) {
        ctx.body = err;
    }
  }

  // 删除
  async removeImage() {
      const {ctx} = this;
      const {imgPath} = ctx.request.body;
      try {
        // 填写Object完整路径。Object完整路径中不能包含Bucket名称。
        let result = await client.delete(imgPath);
        ctx.body = {result};
      } catch (e) {
        ctx.body = e;
      }
  }
  // 获取所有的图片
  async getAllImage() {
      const {ctx} = this;
      let result = await client.list();
      ctx.body = {data: result.objects}
  }
}

module.exports = AliOssController;
